// https://leetcode-cn.com/problems/daily-temperatures/
//
// 739. Daily Temperatures
//
// Given an array of integers temperatures represents the daily temperatures,
// return an array answer such that answer[i] is the number of days you have
// to wait after the ith day to get a warmer temperature. If there is no future day
// to which this is possible, keep answer[i] === 0 instead
function dailyTemperatures(temperatures: number[]): number[] {
    const n = temperatures.length;
    const result: number[] = new Array(n).fill(0);
    const stack: number[] = [];
    for (let i = 0; i < n; i++) {
        const temp = temperatures[i];
        while (stack.length && temp > temperatures[peek(stack)]) {
            const j = stack.pop() as number;
            result[j] = i - j;
        }
        stack.push(i);
    }

    function peek(stack: number[]): number {
        return stack[stack.length - 1];
    }
    return result;
}
